/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.sim.controller;

import cn.iot.card.open.context.LoginContextHolder;
import cn.iot.card.open.context.model.LoginUser;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.modular.entity.AccountCard;
import cn.iot.card.open.modular.entity.Card;
import cn.iot.card.open.modular.entity.UserGroupRl;
import cn.iot.card.open.modular.log.ApiBussinessLog;
import cn.iot.card.open.modular.service.AccountCardService;
import cn.iot.card.open.modular.service.CardService;
import cn.iot.card.open.modular.service.UserService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.pojo.page.LayuiPageInfo;
import cn.iot.card.open.sim.dto.siminfo.ManagerGroupVoiceWhiteDTO;
import cn.iot.card.open.sim.service.SimInfoForVoiceService;
import cn.iot.card.open.util.CommonUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 物联卡综合信息---语音
 *
 * @author zhanlele
 * @date 20200316 17:32:56
 * @modify 20200316 zhanlele v1.1.2 创建
 * @since v1.1.2
 */
@Controller
@RequestMapping("/simInfoForVoice")
public class SimInfoForVoiceController {

    /**
     * 页面路径地址前缀
     */
    private static String PREFIX = "/modular/sim/general/voice/";

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    @Autowired
    private AccountCardService accountCardService;

    @Autowired
    private SimInfoForVoiceService simInfoForVoiceService;

    /**
     * 套餐内语音用量
     *
     * @param msisdn 物联卡号码
     * @param iccid 集成电路卡识别码
     * @param imsi 国际移动用户识别码
     * @return 返回 layui page info
     * @throws Exception
     * @author zhanlele
     * @date 20200316 17:32:13
     * @modify 20200316 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/packageVoiceUsed")
    @ResponseBody
    public LayuiPageInfo packageVoiceUsed(@RequestParam(required = false) String msisdn,
                                          @RequestParam(required = false) String iccid,
                                          @RequestParam(required = false) String imsi) throws Exception {
        if (StringUtils.isBlank(msisdn) && StringUtils.isBlank(iccid) && StringUtils.isBlank(imsi)) {
            return LayuiPageFactory.createEmptyPageInfo();
        }

        Card card = cardService.verifyCardInfo(msisdn, iccid, imsi);
        if (null == card) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForVoiceService.packageVoiceUsed(accountId, transId, msisdn, iccid, imsi);
    }

    /**
     * 个人智能网语音信息
     *
     * @param msisdn 物联卡号码
     * @param iccid 集成电路卡识别码
     * @param imsi 国际移动用户识别码
     * @return 返回 layui page info
     * @throws Exception
     * @author zhanlele
     * @date 20200315 21:05:57
     * @modify 20200315 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/smartVoiceInfo")
    @ResponseBody
    public LayuiPageInfo smartVoiceInfo(@RequestParam(required = false) String msisdn,
                                        @RequestParam(required = false) String iccid,
                                        @RequestParam(required = false) String imsi) throws Exception {
        if (StringUtils.isBlank(msisdn) && StringUtils.isBlank(iccid) && StringUtils.isBlank(imsi)) {
            return LayuiPageFactory.createEmptyPageInfo();
        }

        Card card = cardService.verifyCardInfo(msisdn, iccid, imsi);
        if (null == card) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForVoiceService.smartVoiceInfo(accountId, transId, msisdn, iccid, imsi);
    }

    /**
     * 群组成员语音白名单查询
     *
     * @param msisdn 物联卡号码
     * @param iccid 集成电路卡识别码
     * @param imsi 国际移动用户识别码
     * @param groupId 群组ID
     * @return 返回 layui page info
     * @throws Exception
     * @author zhanlele
     * @date 20200315 21:48:15
     * @modify 20200315 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/groupVoiceWhite")
    @ResponseBody
    public LayuiPageInfo groupVoiceWhite(@RequestParam(required = false) String msisdn,
                                         @RequestParam(required = false) String iccid,
                                         @RequestParam(required = false) String imsi, String groupId) throws Exception {
        if (StringUtils.isBlank(msisdn) && StringUtils.isBlank(iccid) && StringUtils.isBlank(imsi)) {
            return LayuiPageFactory.createEmptyPageInfo();
        }

        Card card = cardService.verifyCardInfo(msisdn, iccid, imsi);
        if (null == card) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForVoiceService.groupVoiceWhite(accountId, transId, groupId);
    }

    /**
     * 跳转群组成员语音白名单管理页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200315 21:59:19
     * @modify 20200315 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toManagerGroupVoiceWhite")
    public String toManagerGroupVoiceWhite() {
        return PREFIX + "managerGroupVoiceWhite.html";
    }

    /**
     * 群组成员语音白名单管理
     *
     * @param managerGroupVoiceWhiteDTO
     * @return 返回 response data
     * @throws Exception
     * @author zhanlele
     * @date 20200315 22:43:23
     * @modify 20200315 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/managerGroupVoiceWhite")
    @ApiBussinessLog(value = "群组成员语音白名单管理")
    @ResponseBody
    public ResponseData managerGroupVoiceWhite(ManagerGroupVoiceWhiteDTO managerGroupVoiceWhiteDTO) throws Exception {
        String cardType = managerGroupVoiceWhiteDTO.getCardType();
        String cardNum = managerGroupVoiceWhiteDTO.getCardNum();
        Card card = cardService.findCard(cardType, cardNum);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForVoiceService.managerGroupVoiceWhite(accountId, transId, managerGroupVoiceWhiteDTO);
    }
}
